//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "EnergyCommodity.h"
using namespace Cephei::QL::Experimental::Commodities;
#include <gen/QL/Experimental/Commodities/CommodityType.h>
#include <gen/QL/Experimental/Commodities/Quantity.h>
#include <gen/QL/Experimental/Commodities/PricingError.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Experimental/Commodities/Commodity.h>
using namespace Cephei::QL;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Commodities::CEnergyCommodity::CEnergyCommodity (boost::shared_ptr<QuantLib::EnergyCommodity>& childNative, Object^ owner) : CCommodity(CEnergyCommodity::typeid)
{
#ifdef HANDLE
	_phEnergyCommodity = NULL;
#endif
	_ppEnergyCommodity = &childNative;
    _ppCommodity = new boost::shared_ptr<QuantLib::Commodity> (boost::dynamic_pointer_cast<QuantLib::Commodity> (*_ppEnergyCommodity));
}
Cephei::QL::Experimental::Commodities::CEnergyCommodity::CEnergyCommodity (QuantLib::EnergyCommodity& childNative, Object^ owner) : CCommodity(CEnergyCommodity::typeid)
{
#ifdef HANDLE
	_phEnergyCommodity = NULL;
#endif
	_ppEnergyCommodity = new boost::shared_ptr<QuantLib::EnergyCommodity> (&childNative);
    _ppCommodity = new boost::shared_ptr<QuantLib::Commodity> (boost::dynamic_pointer_cast<QuantLib::Commodity> (*_ppEnergyCommodity));
    _EnergyCommodityOwner = owner;
    _CommodityOwner = owner;
}

Cephei::QL::Experimental::Commodities::CEnergyCommodity::CEnergyCommodity (CEnergyCommodity^ copy) : CCommodity(CEnergyCommodity::typeid)
{
#ifdef HANDLE
	_phEnergyCommodity = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppEnergyCommodity = new boost::shared_ptr<QuantLib::EnergyCommodity> (copy->GetShared());
        _ppCommodity = new boost::shared_ptr<QuantLib::Commodity> (boost::dynamic_pointer_cast<QuantLib::Commodity> (*_ppEnergyCommodity));
    }
}
Cephei::QL::Experimental::Commodities::CEnergyCommodity::CEnergyCommodity (PLATFORM::Type^ t) : CCommodity(CEnergyCommodity::typeid)
{
#ifdef HANDLE
	_phEnergyCommodity = NULL;
#endif
	if (!t->IsSubclassOf(CEnergyCommodity::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CEnergyCommodity::CEnergyCommodity (QuantLib::Handle<QuantLib::EnergyCommodity>& childNative, Object^ owner)  : CCommodity(CEnergyCommodity::typeid)
{
	_phEnergyCommodity = &childNative;
	_ppEnergyCommodity = &static_cast<boost::shared_ptr<QuantLib::EnergyCommodity>>(childNative.currentLink());
    _ppCommodity = new boost::shared_ptr<QuantLib::Commodity> (boost::dynamic_pointer_cast<QuantLib::Commodity> (*_ppEnergyCommodity));
    _EnergyCommodityOwner = owner;
}
Cephei::QL::Experimental::Commodities::CEnergyCommodity::CEnergyCommodity (QuantLib::Handle<QuantLib::EnergyCommodity> childNative)  : CCommodity(CEnergyCommodity::typeid)
{
	_phEnergyCommodity = &childNative;
	_ppEnergyCommodity = &static_cast<boost::shared_ptr<QuantLib::EnergyCommodity>>(childNative.currentLink());
    _ppCommodity = new boost::shared_ptr<QuantLib::Commodity> (boost::dynamic_pointer_cast<QuantLib::Commodity> (*_ppEnergyCommodity));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CEnergyCommodity::CEnergyCommodity (QuantLib::EnergyCommodity childNative)  : CCommodity(CEnergyCommodity::typeid)
{
#ifdef HANDLE
	_phEnergyCommodity = NULL;
#endif
	_ppEnergyCommodity = new boost::shared_ptr<QuantLib::EnergyCommodity> (new QuantLib::EnergyCommodity (childNative));
    _ppCommodity = new boost::shared_ptr<QuantLib::Commodity> (boost::dynamic_pointer_cast<QuantLib::Commodity> (*_ppEnergyCommodity));
}
#endif

Cephei::QL::Experimental::Commodities::CEnergyCommodity::~CEnergyCommodity ()
{
    if (_ppEnergyCommodity != NULL)
    {
	    delete _ppEnergyCommodity;
        _ppEnergyCommodity = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CEnergyCommodity::!CEnergyCommodity ()
{
    if (_ppEnergyCommodity != NULL)
    {
	    delete _ppEnergyCommodity;
    }
}
QuantLib::EnergyCommodity& Cephei::QL::Experimental::Commodities::CEnergyCommodity::GetReference ()
{
    if (_ppEnergyCommodity == NULL) throw REFNEW NativeNullException ();
	return **_ppEnergyCommodity;
}
boost::shared_ptr<QuantLib::EnergyCommodity>& Cephei::QL::Experimental::Commodities::CEnergyCommodity::GetShared ()
{
    if (_ppEnergyCommodity == NULL) throw REFNEW NativeNullException ();
	return *_ppEnergyCommodity;
}
QuantLib::EnergyCommodity* Cephei::QL::Experimental::Commodities::CEnergyCommodity::GetPointer ()
{
    if (_ppEnergyCommodity == NULL) throw REFNEW NativeNullException ();
	return &**_ppEnergyCommodity;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::EnergyCommodity>& Cephei::QL::Experimental::Commodities::CEnergyCommodity::GetHandle ()
{
	if (_phEnergyCommodity == NULL)
	{
		_phEnergyCommodity = new Handle<QuantLib::EnergyCommodity> (*_ppEnergyCommodity);
	}
	return *_phEnergyCommodity;
}
#endif
bool Cephei::QL::Experimental::Commodities::CEnergyCommodity::HasNative () 
{
	return (_ppEnergyCommodity != NULL);
}

Cephei::QL::Experimental::Commodities::ICommodityType^ Cephei::QL::Experimental::Commodities::CEnergyCommodity::CommodityType::get ()
{
    try
    {
    	QuantLib::CommodityType& _rv = (QuantLib::CommodityType&)(*_ppEnergyCommodity)->commodityType ( );   
        Cephei::QL::Experimental::Commodities::CCommodityType^ _nrv = REFNEW Cephei::QL::Experimental::Commodities::CCommodityType (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::IQuantity^ Cephei::QL::Experimental::Commodities::CEnergyCommodity::Quantity::get ()
{
    try
    {
    	QuantLib::Quantity _rv = (QuantLib::Quantity)(*_ppEnergyCommodity)->quantity ( );   
        Cephei::QL::Experimental::Commodities::CQuantity^ _nrv = REFNEW Cephei::QL::Experimental::Commodities::CQuantity (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

